﻿using DFGK_WMS.IRepository;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace DFGK_WMS.WebAPI.Filter
{
    public class LogActionFilter : ActionFilterAttribute
    {
        private readonly ILogger<LogActionFilter> logger;
        private readonly ITInterfaceLogRepository tInterfaceLog;

        public LogActionFilter(ILogger<LogActionFilter> logger, ITInterfaceLogRepository tInterfaceLog)
        {
            this.logger = logger;
            this.tInterfaceLog = tInterfaceLog;
        }
        /// <summary>
        /// 接口记录
        /// </summary>
        /// <param name="context"></param>
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            var inparam = context.ActionArguments;
            string InParam = JsonConvert.SerializeObject(inparam);
            var request = context.HttpContext.Request;
            var url = request.Path;
            var method = request.Method;
            if (method == "POST")
            {
                TInterfaceLog ilog = new TInterfaceLog();
                ilog.Url = url;
                ilog.CallWayStr = method;
                ilog.InParam = InParam;
                ilog.Status = 1;
                // 请求成功
                ilog.CreateDate = DateTime.Now;
                tInterfaceLog.AddInterfacelog(ilog);
            }
            base.OnActionExecuting(context);
        }
    }
}
